package com.xuecheng.education.note.listener;

import com.aliyuncs.exceptions.ClientException;
import com.xuecheng.education.common.entity.note.NoteMessage;
import com.xuecheng.education.common.entity.note.NoteProperties;
import com.xuecheng.education.common.entity.rabbitmq.ExchangeProperties;
import com.xuecheng.education.common.entity.rabbitmq.QueueProperties;
import com.xuecheng.education.common.entity.rabbitmq.RoutingKeyProperties;
import com.xuecheng.education.note.utils.NoteUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * rabbitmq 消息监听类
 *
 * @Author: island
 * @CreateDate: 2020/8/7 14:44
 * @ProjectName: xuecheng-education-parent
 */
@Component
@EnableConfigurationProperties(NoteProperties.class)
public class NoteListener {

    @Autowired
    private NoteUtils noteUtils;

    /**
     * 消息监听方法
     * @param noteMessage 消息实体类
     * @throws ClientException
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = QueueProperties.NOTE_MESSAGE_QUEUE,durable = "true"),
            exchange = @Exchange(value = ExchangeProperties.NOTE_MESSAGE_EXCHANGE
                    ,ignoreDeclarationExceptions = "true"),
            key = {RoutingKeyProperties.NOTE_VERIFY_CODE}
    ))
    public void listenerNote(NoteMessage noteMessage) throws ClientException {

        if (ObjectUtils.isEmpty(noteMessage)) {
            //放弃处理
            return;
        }

        //发送消息
        this.noteUtils.sendNote(noteMessage.getNotePhone(),
                noteMessage.getVerificationCode(),
                noteMessage.getProperties().getSignName(),
                noteMessage.getProperties().getVerifyCodeTemplate());
    }
}
